<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="工单类型" prop="sheetType" label-width="100px">
        <el-select v-model="queryParams.sheetType" placeholder="请选择">
          <el-option
            v-for="item in sheetTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="条件编号" prop="condId">
        <el-input
          v-model="queryParams.condId"
          placeholder="请输入条件编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="逻辑运算ID" prop="logicCode" label-width="100px">
        <el-input
          v-model="queryParams.logicCode"
          placeholder="请输入逻辑运算ID"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="处理结点编号" prop="auditNode" label-width="100px">
        <el-input
          v-model="queryParams.auditNode"
          placeholder="请输入处理结点编号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['wodemand:SHEETCONDITIONS:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wodemand:SHEETCONDITIONS:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wodemand:SHEETCONDITIONS:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="info"
          icon="el-icon-upload2"
          size="mini"
          @click="handleImport"
        >补充判断值</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wodemand:SHEETCONDITIONS:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="SHEETCONDITIONSList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="工单名称ID" align="center" prop="sheetName" />
<!--      <el-table-column label="条件编号" align="center" prop="id" />-->
      <el-table-column label="条件内容" align="center" prop="condText" />
      <el-table-column label="判断条件" align="center" prop="symbolName" />
      <el-table-column label="处理结点编号" align="center" prop="label" />
      <el-table-column label="处理人工号" align="center" prop="auditStaffId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wodemand:SHEETCONDITIONS:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wodemand:SHEETCONDITIONS:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改配置工单条件对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工单名称" prop="condText">
        <el-select v-model="form.sheetTypeId" placeholder="请选择">
          <el-option
            v-for="item in sheetTypes"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
            <span style="float: left">{{ item.label }}</span>
            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
          </el-option>
        </el-select>
        </el-form-item>
        <el-form-item label="录入条件内容" prop="condText">
          <el-input v-model="form.condText" type="textarea" :rows="2" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="运算逻辑" prop="logicCode">
          <el-select v-model="form.logicCode" placeholder="请选择">
            <el-option
              v-for="item in LogicCodes"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >
              <span style="float: left">{{ item.label }}</span>
              <span style="float: right; color: #8492a6; font-size: 13px">{{ item.value }}</span>
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="判断值" prop="">
          <el-input v-model="form.reserved1" type="text" placeholder="请输入判断值" />
        </el-form-item>
        <el-form-item label="工单节点" prop="logicCode">
          <el-select v-model="form.auditNode" placeholder="请选择">
            <el-option
              v-for="dict in woNodes"
              :key="dict.dictValue"
              :label="dict.dictLabel"
              :value="dict.dictValue"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="处理人工号" prop="auditStaffId">
          <el-input v-model="form.auditStaffId" type="text" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-dialog :title="upload.title" :visible.sync="upload.open" width="400px">
      <el-upload
        ref="upload"
        :limit="1"
        accept=".xlsx, .xls"
        :headers="upload.headers"
        :action="upload.url + '?updateSupport=' + upload.updateSupport"
        :disabled="upload.isUploading"
        :on-progress="handleFileUploadProgress"
        :on-success="handleFileSuccess"
        :auto-upload="false"
        drag
      >
        <i class="el-icon-upload"></i>
        <div class="el-upload__text">
          将文件拖到此处，或
          <em>点击上传</em>
        </div>
        <div class="el-upload__tip" slot="tip">
          <!--          <el-checkbox v-model="upload.updateSupport"/>-->
          <!--          是否更新已经存在的员工数据-->
          <el-link type="info" style="font-size:12px" @click="importTemplate">下载模板</el-link>
        </div>
        <div class="el-upload__tip" style="color:red" slot="tip">提示：仅允许导入“xls”或“xlsx”格式文件！</div>
      </el-upload>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitFileForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
    import {
        addSHEETCONDITIONS,
        delSHEETCONDITIONS,
        exportSHEETCONDITIONS,
        getLogicCodes,
        getSHEETCONDITIONS,
        getSheetTypes,
        listSHEETCONDITIONS,
        updateSHEETCONDITIONS,importTemplate
    } from "@/api/wodemand/SHEETCONDITIONS";
    import { getToken } from "@/utils/auth";

    export default {
  name: "SHEETCONDITIONS",
  data() {
    return {
        woNodes:[],
        upload: {
            // 是否显示弹出层（用户导入）
            open: false,
            // 弹出层标题（用户导入）
            title: "",
            // 是否禁用上传
            isUploading: false,

            // 设置上传的请求头部
            headers: {Authorization: "Bearer " + getToken()},
            // 上传的地址
            url: process.env.VUE_APP_BASE_API + "/hr/CONTRACTINFO/importData"
        },
        LogicCodes:[],
        sheetTypes:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 配置工单条件表格数据
      SHEETCONDITIONSList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        sheetType: undefined,
        pageNum: 1,
        pageSize: 10,
        sheetTypeId: undefined,
        condId: undefined,
        condText: undefined,
        logicCode: undefined,
        logicSymbol: undefined,
        auditNode: undefined,
        auditStaffId: undefined,
        reserved1: undefined,
        reserved2: undefined,
        reserved3: undefined,
        reserved4: undefined,
        reserved5: undefined,
        reserved6: undefined,
        reserved7: undefined,
        reserved8: undefined,
        reserved9: undefined,
        reserved10: undefined,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        sheetTypeId: [
          { required: true, message: "工单类型ID不能为空", trigger: "blur" }
        ],
        auditNode: [
          { required: true, message: "处理结点编号   (1主管 2副总)不能为空", trigger: "blur" }
        ],
        auditStaffId: [
          { required: true, message: "处理人工号不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getList();
      this.getSheetTypeSV();
      this.getLogicCodesI();
      this.getDicts("wo_node").then(response => {
          this.woNodes = response.data;
      });
  },
  methods: {
      handleImport() {
          this.upload.title = "判断值导入";
          this.upload.open = true;
      },
      /** 下载模板操作 */
      importTemplate() {
          importTemplate().then(response => {
              this.download(response.msg);
          });
      },
      // 文件上传中处理
      handleFileUploadProgress(event, file, fileList) {
          this.upload.isUploading = true;
      },
      // 文件上传成功处理
      handleFileSuccess(response, file, fileList) {
          this.upload.open = false;
          this.upload.isUploading = false;
          this.$refs.upload.clearFiles();
          this.$alert(response.msg, "导入结果", {dangerouslyUseHTMLString: true});
          this.getList();
      },
      // 提交上传文件
      submitFileForm() {
          this.$refs.upload.submit();
      },
      getLogicCodesI: function () {
          getLogicCodes().then(response => {
              this.LogicCodes = response.data;
          })
      },
      getSheetTypeSV:function() {
      debugger
          getSheetTypes().then(response => {
              this.sheetTypes = response.data;
          })
      },
    /** 查询配置工单条件列表 */
    getList() {
      this.loading = true;
      listSHEETCONDITIONS(this.queryParams).then(response => {
        this.SHEETCONDITIONSList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sheetTypeId: undefined,
        condId: undefined,
        condText: undefined,
        logicCode: undefined,
        logicSymbol: undefined,
        auditNode: undefined,
        auditStaffId: undefined,
        reserved1: undefined,
        reserved2: undefined,
        reserved3: undefined,
        reserved4: undefined,
        reserved5: undefined,
        reserved6: undefined,
        reserved7: undefined,
        reserved8: undefined,
        reserved9: undefined,
        reserved10: undefined,
        id: undefined
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加配置工单条件";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getSHEETCONDITIONS(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改配置工单条件";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateSHEETCONDITIONS(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addSHEETCONDITIONS(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除配置工单条件编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delSHEETCONDITIONS(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有配置工单条件数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportSHEETCONDITIONS(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    }
  }
};
</script>
